package com.dinghui.ebookbag.module.middle.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.dinghui.ebookbag.R;
import com.dinghui.ebookbag.config.Constants;
import com.dinghui.ebookbag.enity.VideoResource;

import java.util.List;

public class VideoResourceAdapter extends RecyclerView.Adapter<VideoResourceAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    private Context mContext;
    private List<VideoResource> mDatas;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    //创建构造参数
    public VideoResourceAdapter(Context context , List<VideoResource> datas){
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(context);
    }

    //创建ViewHolder
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycleview_item_video_resource , parent , false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    //绑定ViewHolder
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        String imgUrl = Constants.API.BASE_IMAGE + mDatas.get(position).getVideoImage();
        //holder.videoResourceImage.setImageURI();

        //通过 RequestOptions 对象来设置Glide的配置
        RequestOptions options = new RequestOptions()
                //.circleCrop()                      // 设置图片变换为圆角
                .placeholder(R.mipmap.ic_launcher)   // 设置站位图
                .error(R.mipmap.ic_launcher_round);  // 设置加载失败的错误图片

        // Glide.with 会创建一个图片的实例，接收 Context、Activity、Fragment
        Glide.with(mContext)
                .load(imgUrl)                        // 指定需要加载的图片资源，接收 Drawable对象、网络图片地址、本地图片文件、资源文件、二进制流、Uri对象等等
                .apply(options)                      // 指定配置
                .into(holder.videoResourceImage);    // 用于展示图片的ImageView

        holder.videoResourceTitle.setText(mDatas.get(position).getTitle());

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 调用接口的回调方法
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }



    class MyViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        ImageView videoResourceImage;
        TextView videoResourceTitle;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.card_view);
            videoResourceImage = (ImageView) itemView.findViewById(R.id.iv_video_resource_image);
            videoResourceTitle = (TextView) itemView.findViewById(R.id.tv_video_resource_title);
        }
    }

    /**
     * ItemView点击事件回调接口
     */
    public interface OnItemClickListener {
        void onItemClick(int position);
    }









}
